/**
 * @projectDescription This file is the base of MUNS.
 * MUNS copyright, 2010
 * @author xkx
 */
if (!MUN)
{
	var MUN = new Object();
}
MUN.version = "0.1";
MUN.copyrightHTML = "Model United Nations Secretary <br/> Version "+MUN.version+".<br/>Author: Kexiang Xu & William Huang <br/> Contact:<br/> <a href='mailto:modelunsecretary@gmail.com'>modelunsecretary@gmail.com</a><br/>For non-commercial use only.";

//utils
MUN.util = new Object();
MUN.util.nation_names = ["Afghanistan","Albania","Algeria","Andorra","Angola","Antigua & Barbuda","Argentina","Armenia","Australia","Austria","Azerbaijan","Bahamas","Bahrain","Bangladesh","Barbados","Belarus","Belgium","Belize","Benin","Bhutan","Bolivia","Bosnia & Herzegovina","Botswana","Brazil","Brunei","Bulgaria","Burkina Faso","Burundi","Cambodja","Cameroon","Canada","Cape Verde","Central African Republic","Chad","Chile","China","Colombia","Comoros","Congo-Brazzaville","Congo-Kinshasa","Costa Rica","Denmark","Djibouti","Dominica","Dominican Republic","Ecuador","Egypt","El Salvador","Estonia","Ethiopia","Fiji","Finland","France","Gabon","Gambia","Georgia","Germany","Ghana","Greece","Guatemala","Guinea","Guinea-Bissau","Guyana","Haiti","Honduras","Hungary","Iceland","India","Indonesia","Iran","Iraq","Ireland","Israel","Italy","Jamaica","Japan","Jordan","Kazakhstan","Kenya","Kiribati","Democratic People's Republic of Korea","Republic of Korea", "Kuwait","Kyrgyzstan","Laos","Latvia","Lebanon","Liberia","Libya","Liechtenshein","Luxembourg","Macedonia","Madagascar","Malawi","Malaysia","Maldives","Mali","Malta","Marshall Islands","Mauritania","Mauritius","Mexico","Micronesia","Moldova","Monaco","Mongolia","Montenegro","Morocco","Mozambique","Myanmar","Namibia","Nepal","Netherlands","New Zealand","Nicaragua","Niger","Nigeria","Norway","Oman","Pakistan","Palau","Palestine","Panama","Papua New Guinea","Paraguay","Peru","Philippines","Poland","Portugal","Qatar","Romania","Russian Federation","Rwanda","Samoa","San Marino","Saudi Arabia","Senegal","Serbia","Sierra Leone","Singapore","Slovakia","Slovenia","Solomon Islands","Somalia","South Africa","Spain","Sri Lanka","Sudan","Swaziland","Sweden","Switzerland","Syria","Tajikistan","Tanzania","Thailand","Tonga","Trinidad & Tobago","Tunisia","Turkey","Turkmenistan","Tuvalu","Uganda","Ukraine","United Arab Emirates","United Kingdom","United States of America","Uruguay","Uzbekistan","Vanutau","Vatican City","Venezuela","Vietnam","Yemen","Zambia","Zimbabwe"];
MUN.util.nationflagspath = [];
for (var i=0; i<MUN.util.nation_names.length; i++)
{
	MUN.util.nationflagspath[MUN.util.nation_names[i]] = "flags/"+escape(MUN.util.nation_names[i])+".png";
}
MUN.util.nationflagspath['empty'] = "flags/empty_flag.png";
MUN.util.tickpath="img/silk/tick.png";
MUN.util.crosspath="img/silk/cross.png";
MUN.util.emptypath="img/silk/empty.png";
/*MUN.util.autocompletesetting = function(autocomplete)
{
	nationnames = MUN.util.nation_names;
	for (var i=0; i<nationnames.length; i++)
	{
		autocomplete.addValues([i, nationnames[i], ("<img src='"+MUN.util.nationflagspath[nationnames[i]]+"' class='p'/>"+nationnames[i]), ("<img src='"+MUN.util.nationflagspath[nationnames[i]]+"' class='q'/>"+nationnames[i])]);
	}
};
*/
MUN.util.autocomplete_setting = function(nation_name)
{
	var choice = new Element('li');
	new Element('img', {'src' : MUN.util.nationflagspath[nation_name], 'class' : 'p' }).inject(choice);
	new Element('span', {'html': this.markQueryValue(nation_name)}).inject(choice);
	choice.inputValue = nation_name;
	this.addChoiceEvents(choice).inject(this.choices);
}

//status
MUN.status = {
	motion_ok : false,
	submit_ok : false,
	set_motion_ok : function()
	{
		MUN.status.motion_ok = true;
		MUN.module.motion.enable();
	},
	set_submit_ok : function()
	{
		MUN.status.submit_ok = true;
		MUN.module.submit.enable();
	},
	status_update : function(msg)
	{
		switch (msg)
		{
			case 'out-of-rollcall': MUN.status.set_motion_ok();
									MUN.status.set_add_p_nation_ok();
									MUN.module.motion.change_menu(msg);
		}
	},
	add_s_nation_ok : false,
	add_p_nation_ok : false,
	set_add_s_nation_ok : function()
	{
		$('add-s-nation').disabled = false;
	},
	set_add_p_nation_ok : function()
	{
		$('add-p-nation').disabled = false;
	},
	set_add_s_nation_diabled : function()
	{
		$('add-s-nation').disabled = true;
	},
	set_add_p_nation_diabled : function()
	{
		$('add-p-nation').disabled = true;
	}
	
}
//languages
MUN.lang = new Object();
MUN.lang.current_lang =  new Object();
MUN.lang.en = new Object();
MUN.lang.en = {
	'chinese' : "Chinese",
	'english' : "English",
	'speakers-list' : "Speakers' List",
	'add-to-s-list' : "Add to Speakers' List",
	'add-to-s-list' : "Add to Present List"
};
MUN.lang.current_lang = MUN.lang.en;
MUN.lang.change = function(lang)
	{
	MUN.lang.current_lang = MUN.lang[lang];
	//...
	};

//common modules
MUN.module = new Object();
MUN.module.dialog = new Object();
MUN.module.dialog.create = function(title,id,buttons)
{
	this.dialog = new Element('div',{'class':'_dialog'});
	this.title = new Element('div',{'text':title,'class':'_dialog-title'}).inject(this.dialog);
	this.content = $(id).inject(this.dialog);
	this.footer = new Element('div',{'class':'_dialog-footer'}).inject(this.dialog);
	this.buttons = new Object();
	for(var i in buttons)
	{
		if (typeOf(buttons[i]) == "element") 
		{
			this.buttons[i] = new Element('button', {
				'text': i
			}).inject(this.footer);
			this.buttons[i].eventlistener = buttons[i];
		}
	}
	this.dialog_box = new MUI.ModalElementBox(this.dialog);
	this.show = function()
	{
		this.dialog_box.show();
		if (!this.show.notfirsttime) {
			for (var i in this.buttons) {
				if (typeOf(this.buttons[i]) == "element")
					this.buttons[i].addEvent('click', this.buttons[i].eventlistener);
			}
		this.show.notfirsttime = true;
		}
	};
	this.hide = function(){this.dialog_box.hide();};
	return true;
};
MUN.module.menu = new Object();
MUN.module.menu.create = function(id,classname,items,target)//target(id): optional
{
	this.container = new Element('div',{'class':'_menu-container '+classname, 'id':id});
	this.ul = new Element('ul').inject(this.container);
	this.items = new Array();
	for(var i in items)
	{
		if (typeof(items[i]) == "function") 
		{
			this.items[i] = new Element('a', {
			'text': i
			}).inject(new Element('li').inject(this.ul));
			this.items[i].eventlistener = items[i];
		}
	}
	if(target)
	{
		this.target = $(target);
		this.target.addEvent('mousedown',this.target_mousedown);
	}
	
	this.show = function(target)//target: optional
	{	
		console.info('show');
		this.no_hide = true;
		if (!this.show.notfirsttime) 
			this.container.inject(document.body);

		if(target)
		{
			this.target = $(target);
		}
		//thanks CMmenu
		var doc_coord = $(document).getCoordinates();
		var container_coord = this.container.getCoordinates();
		var target_coord = this.target.getCoordinates();
		
		var container_pos = {'x':0,'y':0}; //output
		//vertical
		if (container_coord.width + target_coord.left > doc_coord.width) 
		{container_pos.x = doc_coord.width - container_coord.width;}
		else
		{container_pos.x = target_coord.left;}
		//horizontal
		if (container_coord.height + target_coord.bottom > doc_coord.height) 
		{container_pos.y = target_coord.top - container_coord.height;}
		else
		{container_pos.y = target_coord.bottom;}
		//set pos
		this.container.setPosition(container_pos);
		this.container.style.visibility = "visible";
		
		if (!this.show.notfirsttime)
		{
			for (i in this.items)
				{
					if (typeOf(this.items[i]) == "element")
					{
						this.items[i].addEvent('click', this.onclick.bind(this,i));
					}
				}
			this.show.notfirsttime = true;
		}
		this.container.focus();
	};
	this.hide = function()
	{	console.info('hide');
		if(this.no_hide != true)
		{
			this.container.setPosition({'x':-100000,'y':-100000});
		}
		this.no_hide = false;
	};
	this.onclick = function(item)
	{
		console.info('click');
		this.no_hide = true;
		this.hide();
		this.items[item].eventlistener.call();
	}
	this.add_items = function(items)
	{
		for(var i in items)
		{
			if (typeof(items[i]) == "function") 
			{
				this.items[i] = new Element('a', {
			'text': i
			}).inject(new Element('li').inject(this.ul));
				this.items[i].eventlistener = items[i];
			}
		}
		if(this.show.notfirsttime)
		{
			for (i in this.items) {
				if (typeOf(this.items[i]) == "element") 
				{
					this.items[i].addEvent('click', this.onclick.bind(this,i));
				}
			}
		}
	};
	this.target_mousedown = function()
	{
		if(this.target_mousedown.isshown) {
			
			return;
		}
	};
	this.no_hide = true;
	document.addEvent('click', this.hide.bind(this));
	return true;
};
